﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Atlandiz.Data;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class ListeCihazMarka : DevExpress.XtraEditors.XtraForm
    {
        public bool ForSelection = false;

        public ListeCihazMarka()
        {
            InitializeComponent();
        }

        public void ChzMarkaYukle()
        {
            _lwCihazMarkalari.Items.Clear();

            DataTable dt = Army.SQL().GetDataTable("Select * from CihazMarka where isDeleted=0");

            foreach(DataRow item in dt.Rows)
            {
                ListViewItem ls = new ListViewItem(item[0].ToString());
                ls.SubItems.Add(item[1].ToString());

                _lwCihazMarkalari.Items.Add(ls);
            }

        }

        private void CihazMarkaLst_Load(object sender, EventArgs e)
        {
            Army.EventConfirm = false;
            ChzMarkaYukle();
        }
        
     
        public void KDuzenle()
        {
            FormCihazMarka chzMarkaFormu = new FormCihazMarka();
            chzMarkaFormu.KayID = int.Parse(_lwCihazMarkalari.SelectedItems[0].SubItems[0].Text);
            chzMarkaFormu.ShowDialog();
            ChzMarkaYukle();
        }

        private void düzenleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(_lwCihazMarkalari.SelectedItems.Count > 0)
            {
                KDuzenle();
            }
            else
            {
                MessageBox.Show("Lütfen, düzenlenecek kaydı seçiniz...");
            }
        }

        private void silToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_lwCihazMarkalari.SelectedItems.Count > 0)
            {
                PopupAreYouSure SilOnay = new PopupAreYouSure();
                Army.EventMessage = "Seçilen Marka Silinecek,\nEmin misiniz?";
                SilOnay.ShowDialog();

                if (Army.EventConfirm)
                {
                    Army.SQL().cmd("Update CihazMarka SET isDeleted=1 where cmID=" + _lwCihazMarkalari.SelectedItems[0].SubItems[0].Text.ToString());
                    MessageBox.Show("Seçilen Marka Silindi.", "Kayıt Silindi.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ChzMarkaYukle();
                    //sil komutu
                }
            }



            //if(_lwCihazMarkalari.SelectedItems.Count>0)
            //{
            //    DialogResult sonuc;
            //    sonuc = MessageBox.Show("Kaydı silmek istediğinize emin misiniz?", "Dikkat!", MessageBoxButtons.YesNo);
            //    if(sonuc== DialogResult.Yes)
            //    {
            //        SQL.cmd("Update CihazMarka set isDeleted=1 where cmID="+ _lwCihazMarkalari.SelectedItems[0].SubItems[0].Text);
            //        MessageBox.Show("Kayıt, başarı ile silinmiştir.");
            //        ChzMarkaYukle();
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("Lütfen, silinecek kaydı seçin");
            //}
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (_lwCihazMarkalari.SelectedItems.Count > 0 & ForSelection)
            {
                Army.SecilenID = int.Parse(_lwCihazMarkalari.SelectedItems[0].SubItems[0].Text);
                Army.EventConfirm = true;
                this.Close();
            }
        }

        private void _btnYeni_Click(object sender, EventArgs e)
        {
            FormCihazMarka cm = new FormCihazMarka();
            cm.ShowDialog();
            ChzMarkaYukle();
        }

        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void _btnListeGuncelle_Click(object sender, EventArgs e)
        {
            ChzMarkaYukle();
        }

        private void _btnAra_Click(object sender, EventArgs e)
        {
            String Sorgu = "Select cmID, ChzMarka \n"+
                "from CihazMarka \n"+
                "where isDeleted=0 and cmID>0 and \n"+ 
                "cmID like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or \n"+
                "ChzMarka like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%'  ";

                _lwCihazMarkalari.Items.Clear();

                DataTable dt = Army.SQL().GetDataTable(Sorgu);

                foreach (DataRow item in dt.Rows)
                {
                    ListViewItem ls = new ListViewItem(item[0].ToString());
                    ls.SubItems.Add(item[1].ToString());

                    _lwCihazMarkalari.Items.Add(ls);
                }

                _txtAraString.Focus();
        }

    }
}